package com.hsxxherp.key.support.i18n.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hsxxherp.key.common.error.ErrorCode;
import com.hsxxherp.key.common.exception.BusinessException;
import com.hsxxherp.key.support.i18n.mapper.I18nLocaleMapper;
import com.hsxxherp.key.support.i18n.mapper.I18nResourceMapper;
import com.hsxxherp.key.support.i18n.mapper.I18nResourceTemplateMapper;
import com.hsxxherp.key.support.i18n.model.entity.I18nResource;
import com.hsxxherp.key.support.i18n.model.entity.I18nLocale;
import com.hsxxherp.key.support.i18n.model.entity.I18nResourceTemplate;
import com.hsxxherp.key.support.i18n.service.II18nResourceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class I18nResourceServiceImpl extends ServiceImpl<I18nResourceMapper, I18nResource> implements II18nResourceService<I18nResource> {

    private static final Logger logger = LoggerFactory.getLogger(I18nResourceServiceImpl.class);

    @Autowired
    private I18nLocaleMapper i18nLocaleMapper;

    @Autowired
    private I18nResourceTemplateMapper i18nResourceTemplateMapper;

    @Override
    public String getMessageByLocaleCodeAndResourceCode(String localeCode, String resourceCode) {
        if (Objects.isNull(localeCode) || Objects.isNull(resourceCode)) {
            logger.error("语种代码或资源代码为空，无法获取国际化消息。语种代码: {}, 资源代码: {}", localeCode, resourceCode);
            throw new BusinessException("语种代码和资源代码不能为空").setCode(ErrorCode.FAIL);
        }

        try {
            I18nResource resource = baseMapper.selectByLocaleCodeAndResourceCode(resourceCode, localeCode);
            String message = resource != null ? resource.getMessage() : null;
            logger.info("根据语种代码 {} 和资源代码 {} 获取到消息: {}", localeCode, resourceCode, message);
            return message;
        } catch (Exception e) {
            logger.error("根据语种代码和资源代码获取消息时出现异常，语种代码: {}, 资源代码: {}", localeCode, resourceCode, e);
            throw new BusinessException("获取国际化消息失败", e).setCode(ErrorCode.FAIL);
        }
    }

    @Override
    public Map<String, String> getAllMessages() {
        try {
            List<I18nResource> resources = baseMapper.selectAllResources();
            Map<String, String> messageMap = new HashMap<>();

            for (I18nResource resource : resources) {
                String localeCode = convertToLocaleCode(resource.getLocaleId());
                String resourceCode = convertToResourceCode(resource.getTemplateId());

                String key = localeCode + "_" + resourceCode;
                messageMap.put(key, resource.getMessage());
            }
            return messageMap;
        } catch (Exception e) {
            logger.error("获取所有国际化消息时出现异常", e);
            throw new BusinessException("获取所有国际化消息失败", e).setCode(ErrorCode.FAIL);
        }
    }

    /**
     * 根据 localeId 转换为 localeCode
     * @param localeId 语种 ID
     * @return 对应的语种代码
     */
    private String convertToLocaleCode(String localeId) {
        if (localeId == null) {
            return null;
        }
        I18nLocale i18nLocale = i18nLocaleMapper.selectById(localeId);
        return i18nLocale != null ? i18nLocale.getLocaleCode() : null;
    }

    /**
     * 根据 templateId 转换为 resourceCode
     * @param templateId 模板 ID
     * @return 对应的资源代码
     */
    private String convertToResourceCode(String templateId) {
        if (templateId == null) {
            return null;
        }
        I18nResourceTemplate i18nTemplate = i18nResourceTemplateMapper.selectById(templateId);
        return i18nTemplate != null ? i18nTemplate.getResourceCode() : null;
    }
}